package com.nanyang.tms.manager.forecast.handler;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.product.ProductRuleDao;
import com.nanyang.tms.domain.forecast.context.CalculateWeightContext;
import com.nanyang.tms.domain.forecast.context.saveforecast.SaveForecastContext;
import com.nanyang.tms.domain.forecast.message.GetTicketNumberMessage;

import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class GetTicketNumberHandler  extends AbstractTransHandler{
	
	@Autowired
	private RedissonClient redissonClient;
	@Autowired
	private ProductRuleDao productRuleDao;
	@Autowired
	private RocketMQTemplate rocketMQTemplate;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		CalculateWeightContext calculateWeightContext=(CalculateWeightContext) context.getInnerContext(CalculateWeightContext.class);
		SaveForecastContext saveForecastContext=(SaveForecastContext)context.getInnerContext(SaveForecastContext.class);
		try {
			String ticketTypeCode = productRuleDao.getTicketTypeCode(calculateWeightContext.getSaleProductCode());
			if(StringUtils.isEmpty(ticketTypeCode)){
				throw new TmsManagerException(TmsExceptionEnum.TICKET_TYPE_IS_NOT_EXIST.getCode(), TmsExceptionEnum.TICKET_TYPE_IS_NOT_EXIST.getMessage());
			}
			 RSemaphore semaphore = redissonClient.getSemaphore(ticketTypeCode);
			 boolean acquire =semaphore.tryAcquire();
			 if(acquire){
				 GetTicketNumberMessage getTicketNumberMessage=GetTicketNumberMessage.builder().forcastCode(saveForecastContext.getForecastCode()).ticketTypeCode(ticketTypeCode).build();
				rocketMQTemplate.asyncSendOrderly(
						"getTicketNumber", MessageBuilder.withPayload(getTicketNumberMessage).setHeader(RocketMQHeaders.KEYS, getTicketNumberMessage.getForcastCode()).build(),
						ticketTypeCode, new SendCallback() {
					
					@Override
					public void onSuccess(SendResult sendResult) {
						log.info(sendResult.toString());
					}
					
					@Override
					public void onException(Throwable e) {
					}
				});
			 }else{
				 throw new TmsManagerException(TmsExceptionEnum.TICKET_NO_IS_NOT_EXIST.getCode(), TmsExceptionEnum.TICKET_NO_IS_NOT_EXIST.getMessage());
			 }
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}

}
